package view;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import model.User;
import dao.UserDAO;

public class AddUserDialog extends JDialog {
    private JTextField usernameField;
    private JPasswordField passwordField;
    private JPasswordField confirmPasswordField;
    private JComboBox<String> roleCombo;
    private UserPanel userPanel;
    
    public AddUserDialog(JFrame parent, UserPanel userPanel) {
        super(parent, "添加新用户", true);
        this.userPanel = userPanel;
        
        setLayout(new GridLayout(5, 2, 5, 5));
        
        // 添加输入框
        add(new JLabel("用户名："));
        usernameField = new JTextField();
        add(usernameField);
        
        add(new JLabel("密码："));
        passwordField = new JPasswordField();
        add(passwordField);
        
        add(new JLabel("确认密码："));
        confirmPasswordField = new JPasswordField();
        add(confirmPasswordField);
        
        add(new JLabel("角色："));
        roleCombo = new JComboBox<>(new String[]{"user", "admin"});
        add(roleCombo);
        
        // 添加按钮
        JButton saveBtn = new JButton("保存");
        JButton cancelBtn = new JButton("取消");
        
        saveBtn.addActionListener(e -> saveUser());
        cancelBtn.addActionListener(e -> dispose());
        
        add(saveBtn);
        add(cancelBtn);
        
        styleComponents();
        
        pack();
        setLocationRelativeTo(parent);
    }
    
    private void saveUser() {
        String username = usernameField.getText().trim();
        String password = new String(passwordField.getPassword());
        String confirmPassword = new String(confirmPasswordField.getPassword());
        String role = (String) roleCombo.getSelectedItem();
        
        // 验证输入
        if (username.isEmpty() || password.isEmpty()) {
            JOptionPane.showMessageDialog(this, "用户名和密码不能为空！", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        if (!password.equals(confirmPassword)) {
            JOptionPane.showMessageDialog(this, "两次输入的密码不一致！", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        UserDAO dao = new UserDAO();
        
        // 检查用户名是否已存在
        if (!dao.checkUsername(username)) {
            JOptionPane.showMessageDialog(this, "用户名已存在！", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        // 创建新用户
        User user = new User(0, username, password, role, null);
        
        if (dao.addUser(user)) {
            JOptionPane.showMessageDialog(this, "添加成功！");
            userPanel.refreshTable();
            dispose();
        } else {
            JOptionPane.showMessageDialog(this, "添加失败！", "错误", JOptionPane.ERROR_MESSAGE);
        }
    }
    
    private void styleComponents() {
        // 设置字体
        Font labelFont = new Font("微软雅黑", Font.PLAIN, 14);
        Font inputFont = new Font("微软雅黑", Font.PLAIN, 14);
        
        // 设置输入框样式
        usernameField.setFont(inputFont);
        passwordField.setFont(inputFont);
        confirmPasswordField.setFont(inputFont);
        roleCombo.setFont(inputFont);
        
        // 设置标签样式
        for (Component comp : getContentPane().getComponents()) {
            if (comp instanceof JLabel) {
                JLabel label = (JLabel) comp;
                label.setFont(labelFont);
            }
        }
        
        // 设置按钮样式
        for (Component comp : getContentPane().getComponents()) {
            if (comp instanceof JButton) {
                JButton button = (JButton) comp;
                button.setFont(labelFont);
                button.setFocusPainted(false);
                button.setBackground(new Color(59, 89, 152));
                button.setForeground(Color.WHITE);
                button.setBorder(BorderFactory.createEmptyBorder(5, 15, 5, 15));
            }
        }
        
        // 设置输入框边框
        Border border = BorderFactory.createCompoundBorder(
            BorderFactory.createLineBorder(new Color(204, 204, 204)),
            BorderFactory.createEmptyBorder(5, 5, 5, 5)
        );
        usernameField.setBorder(border);
        passwordField.setBorder(border);
        confirmPasswordField.setBorder(border);
        roleCombo.setBorder(border);
        
        // 设置窗口大小
        setPreferredSize(new Dimension(400, 250));
    }
} 